using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using LFloatMath.Collision2D;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class TargetSelect_Cub : TargetSelect
    {
        public override ETargetSelectType selectType => ETargetSelectType.Cub;

        public override bool selectByUnit => false;
        
        public LFloat width;
        public LFloat length;

        protected override void OnInitialize(TargetSelectConfig rConfig)
        {
            base.OnInitialize(rConfig);
            if (rConfig.customConfig is TargetSelect_Cub_ConfigData rData)
            {
                this.width = new LFloat(true, rData.width);
                this.length = new LFloat(true, rData.length);
                this.targetDisRange = this.length;
            }
        }

        protected override void OnInitialize(IReadOnlyList<int> rTabConfig)
        {
            base.OnInitialize(rTabConfig);
            if (rTabConfig.Count > 1)
            {
                this.width = new LFloat(true, rTabConfig[0]);
                this.length = new LFloat(true, rTabConfig[1]);
                this.targetDisRange = this.length;
            }
        }

        public override void TrySelect(SelectParam rParam, LVector2 rSelfPos, ref List<Unit> rTargets)
        {
            var rPos = rSelfPos;
            // 获取OBB，偏移中心到矩形中心
            var rProxy = ColliderProxy.Fetch();
            var rAspect = rParam.aspect.normalized;
            rPos += this.length / 2 * rAspect;
            rProxy.Init(CollisionUtil.GetCubePrefab(this.width / 2, this.length / 2, LVector2.up), rPos);
            rProxy.IsStatic = false;
            rProxy.LayerType = 0;
            
            var nAngle = LMath.AngleIntUp(rAspect);
            rProxy.deg = nAngle;

            this.CollisionCheck(rProxy, ref rTargets);
            ColliderProxy.Free(rProxy);
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }
    }
}